package com.blueocean_health.care.management.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.UserAccount;
import com.blueocean_health.care.management.service.LoginService;

@RestController
@RequestMapping("user")
public class LoginController {

	@Autowired
	private LoginService loginService;

	/**
	 * 登录
	 * 
	 * @return
	 */
	@RequestMapping("login")
	public SysResult login(@RequestBody UserAccount userAccount) {
		if (StringUtils.isEmpty(userAccount.getUserName())) {
			return SysResult.error("用户名不能为空");
		} else if (StringUtils.isEmpty(userAccount.getPassword())) {
			return SysResult.error("密码不能为空");
		} else {
			SysResult result = loginService.login(userAccount);
			return result;
		}
	}

	/**
	 * 登录后多身份选择
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("login/select-identity")
	public SysResult selectIdentity(@RequestBody Map<String, String> map, HttpServletRequest request) {
		String teamId = map.get("teamId");
		String token = request.getHeader("token");
		if (StringUtils.isEmpty(token)) {
			return SysResult.error("请求参数异常");
		}
		Map<String, String> paramsMap = new HashMap<>();
		paramsMap.put("teamId", teamId);
		paramsMap.put("token", token);
		return loginService.selectIdentity(paramsMap);
	}
}
